import 'package:flutter/material.dart';
import 'package:myfirstflutter/models/news_list_model.dart';
import 'package:myfirstflutter/pages/zone/news_list.dart';
import 'package:myfirstflutter/utils/common_color.dart';

class ZonePageWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new ZonePageWidgetState();
  }
}

class ZonePageWidgetState extends State<ZonePageWidget>
    with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  TabController mController;
  List<NewsTabItem> tabTitles;

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return new Scaffold(
        appBar: new AppBar(
          title: new Text('园区新闻'),
          bottom: _tabBar(),
        ),
        body: _tabBarView());
  }

  @override
  void dispose() {
    super.dispose();
    mController.dispose();
  }

  @override
  void initState() {
    super.initState();
    tabTitles = [
      NewsTabItem('园区资讯', NewsListType.zoneNews),
      NewsTabItem('活动', NewsListType.zoneActivity),
      NewsTabItem('政策', NewsListType.zonePolicy),
      NewsTabItem('党建服务', NewsListType.zoneService),
    ];

    mController = TabController(
      length: tabTitles.length,
      vsync: this,
    );
  }

  Widget _tabBar() {
    return TabBar(
        //设置tab是否可水平滑动
        isScrollable: true,
        //控制器
        controller: mController,
        unselectedLabelStyle: TextStyle(fontSize: 14),
        //设置tab文字得类型
        labelStyle: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
        //设置tab选中得颜色
        labelColor: textBlueColor,
        //设置tab未选中得颜色
        unselectedLabelColor: Color(0xFF4A4A4A),
        //设置自定义tab的指示器，CustomUnderlineTabIndicator
        //若不需要自定义，可直接通过
        indicator: null,
        tabs: tabTitles.map((item) {
          return Tab(text: item.title);
        }).toList());
  }

  Widget _tabBarView() {
    return TabBarView(
      controller: mController,
      children: tabTitles.map((item) {
        return Container(
            child: new NewsListWiget(
          listType: item.type,
          itemClicked: (type) {
            print('page中点了 $type');
          },
        ));
      }).toList(),
    );
  }
}
